package com.zhijiang.hospital.controller;


import com.zhijiang.config.result.R;
import com.zhijiang.config.webConfig.PassToken;
import com.zhijiang.hospital.entity.SysUser;
import com.zhijiang.hospital.entity.dto.EmployeeDto;
import com.zhijiang.hospital.mapper.EmployeeDtoMapper;
import com.zhijiang.hospital.service.UserService;
import com.zhijiang.util.PasswordUtil;
import com.zhijiang.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 * fan
 * 员工相关所有人公开可访问
 */
@RestController
@Api(value = "用户", tags = {"用户"})
@RequestMapping("user")
public class UserController {
    @Autowired
    UserService userService;
    @Autowired
    EmployeeDtoMapper employeeDtoMapper;

    //登录
    @PostMapping("/login")
    @ApiModelProperty(name = "用户登录接口")
    @PassToken
    public Result login(@RequestBody SysUser sysUser) throws Exception {
        String password = PasswordUtil.desEncrypt(sysUser.getPassword());
        sysUser.setPassword(password);
        HashMap<String, String> login = userService.login(sysUser);
        List<EmployeeDto> middleLevelLeaders = employeeDtoMapper.findMiddleLevelLeaders();
        List<String> collect = middleLevelLeaders.stream().map(EmployeeDto::getEmplCode).collect(Collectors.toList());
        boolean contains = collect.contains(sysUser.getUsername());
        if (!contains) {
            return Result.fail("目前只对中层及以上干部开放");
        }
        return Result.ok(login);
    }

    //登出
    //@PostMapping("/loginout")
    //@ApiModelProperty(name = "登出接口")
    //public Result  loginOut(){
//        userService.loginOut();
    //     return Result.ok("退出成功");
    //}
    @ApiModelProperty(name = "查看用户权限接口")
    @GetMapping("findLimitsOfAuthorityList")
    @PassToken
    public Result findLimitsOfAuthorityList(@RequestParam("emplCode") String emplCode) {
        return Result.ok(userService.findLimitsOfAuthorityList(emplCode));
    }

    @ApiModelProperty("判断是否为中层干部")
    @GetMapping("findAuthority")
    @PassToken
    public Result findAuthority(String emplCode) {
        boolean authority = userService.findAuthority(emplCode);
        return Result.ok(authority);
    }

    @GetMapping("findEmployee")
    @PassToken
    public R findEmployee(String emplCode) {
        return R.ok(employeeDtoMapper.findEmployee(emplCode));
    }

    @GetMapping("test")
    @PassToken
    public R test() {
        return R.ok("helloWord");
    }


    @PostMapping("/Hislogin")
    @ApiModelProperty(name = "用户登录接口")
    @PassToken
    public Result hislogin(@RequestBody SysUser sysUser) throws Exception {
        HashMap<String, String> login = userService.login(sysUser);
        List<EmployeeDto> middleLevelLeaders = employeeDtoMapper.findMiddleLevelLeaders();
        List<String> collect = middleLevelLeaders.stream().map(EmployeeDto::getEmplCode).collect(Collectors.toList());
        boolean contains = collect.contains(sysUser.getUsername());
        if (!contains) {
            return Result.fail("目前只对中层及以上干部开放");
        }
        return Result.ok(login);
    }


}
